package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.floor.vo.AddFloorReqVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.floor.vo.AddFloorRespVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.floor.vo.EditFloorRespVo;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomFloorMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.Apartment;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomFloor;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomFloorRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseRepository;
import com.xiaoshuidi.cloud.module.rooms.service.ApartmentService;
import com.xiaoshuidi.cloud.module.rooms.service.RoomFloorService;
import com.xiaoshuidi.cloud.module.rooms.service.RoomHouseService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RoomFloorServiceImpl extends ServiceImpl<RoomFloorMapper, RoomFloor> implements RoomFloorService {

    @Autowired
    private RoomFloorMapper roomFloorMapper;

    @Lazy
    @Autowired
    private ApartmentService apartmentService;
    @Autowired
    private RoomHouseService roomHouseService;
    @Autowired
    private RoomHouseRepository roomHouseRepository;
    @Autowired
    private RoomFloorRepository roomFloorRepository;

    /**
     * 批量添加楼层
     * @param roomFloors
     * @return
     */
    @Override
    public CommonResult<Boolean> insertBatch(List<RoomFloor> roomFloors) {
        Assert.isTrue(ArrayUtil.isNotEmpty(roomFloors),"楼层列表不能为空");
        roomFloorMapper.insertBatch(roomFloors);
        return CommonResult.success(true);
    }


    /**
     * 修改楼层信息
     * @param floorId 楼层id
     * @param num 楼层
     * @param userId 登录用户
     * @return
     */
    @Override
    public CommonResult<EditFloorRespVo> edit(@NonNull Long floorId, @NonNull Integer num, Long userId) {
        RoomFloor roomFloor = roomFloorRepository.getById(floorId);
        Assert.isTrue(ObjectUtil.isNotEmpty(roomFloor),"楼层信息不存在");
        roomFloor.setNum(num);
        roomFloor.setName(num + "楼");
        roomFloorRepository.updateById(roomFloor);
        EditFloorRespVo respVo = EditFloorRespVo.builder().floorNum(roomFloor.getNum()).floorName(roomFloor.getName()).floorId(floorId).build();
        return CommonResult.success(respVo);
    }

    /**
     * 删除楼层，同时删除楼层下所有房间
     * @param floorId 楼层id
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> delete(Long floorId, Long userId) {
        RoomFloor roomFloor = roomFloorRepository.getById(floorId);
        Assert.isTrue(ObjectUtil.isNotEmpty(roomFloor),"楼层信息不存在");
        // 删除楼层
        roomFloorRepository.removeById(roomFloor);
        // 删除房间
        roomHouseRepository.delByFloorId(floorId);
        return CommonResult.success(Boolean.TRUE);
    }
}
